<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div>
    <div class="contain">
      <el-card class="login-card">
        <!-- header是card组件提供的插槽名字 -->
        <template v-slot:header>
          <h3>黑马面经运营后台</h3>
        </template>
        <el-form
          label-position="top"
          label-width="80px"
          :model="user"
          ref="form"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="user.password"></el-input>
          </el-form-item>
        </el-form>

        <el-row type="flex" justify="center" class="login-button">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button>重置</el-button>
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
//   引入
import { login } from "@/api/user";

export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 登录模块
    login() {
      this.$refs.form.validate(async (bool) => {
        console.log(this.$refs.form);
        if (bool) {
          const res = await login(this.user);
          console.log("校验通过", res);
          // 存一份token在这里
          this.$store.commit("user/saveToken", res.data.data.token);
          //   跳转到首页
          this.$router.push("/");
        } else {
          // console.log('登录失败')
          return false;
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
h3 {
  text-align: center;
  margin: 0;
}
.login-card {
  width: 500px;
  //样式穿透
  ::v-deep .el-card__header {
    background-color: #727cf5;
    color: white;
  }

  .el-button {
    width: 70px;
  }
}
</style>
